/**
 * @Author zhengyuefan
 * @Date 2024/10/4
 */
public class Solution {
    /**
     * 递归的删除链表中的元素
     * @param head
     * @param val
     * @return
     */
    public static ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode res = removeElements(head.next, val);
        if (head.val == val) {
            return res;
        } else {
            head.next = res;
            return head;
        }
    }

    public static void main(String[] args) {
        int arr[] = {2, 4, 2, 2};
        ListNode listNode = new ListNode(arr);
        System.out.println("原链表:" + listNode);
        ListNode res1 = removeElements(listNode, 2);
        System.out.println("删除元素2: " + res1);

    }
}
